package com.chonry.web2.controller;

import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import com.chonry.web2.bean.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

@RestController
@RequestMapping("/")
public class LoginController {

    @PostMapping("/login")
    public Result login(@RequestParam("username") String name, @RequestParam("password") String pass,
                        HttpServletRequest request) {
        String token = "";
        if (!"admin".equals(name)) {
            return new Result(1000,"找不到该用户");
        }
        if (!"1234".equals(pass)) {
            return new Result(1000,"密码错误");
        }
        Date date = new Date();
        token = Jwts.builder().setSubject(name).claim("customerId", 10084).setIssuedAt(date).setExpiration(new Date(date.getTime()+30000))
                .signWith(SignatureAlgorithm.HS256, "base64EncodedSecretKey").compact();
        return new Result(token);
    }
}